var request = require('request');
var https = require('https');
var cfg  = require('../cfg/cfg.js')

var tushare = {}

module.exports = tushare;

 // ts_code trade_date  open  high   low  close  pre_close  change    pct_chg  vol        amount

/*
    code:'000001.SZ'
    start_date:'20190701'
    end_date:'20190705'
*/
tushare.post=function(code,stime,etime,cb){

    var data = {
        api_name: 'daily', 
        token: cfg.token,
        params:{ts_code: code,start_date:stime,end_date:etime},
        fields:'trade_date,open,high,low,close'
    }

    request({
		r:Math.random(),
        url: cfg.url,
        method: "POST",
        json: true,
        headers: {
            "content-type": "application/json",
        },
        body: data
    }, function(error, response, body) {
        if (!error && response.statusCode == 200) {
            // console.log(body) // 请求成功的处理逻辑
			// console.log(body.data.items[0])
			var obj = body.data.items;
			if(obj){
				// for(var i=0;i<obj.length;i++){
				// 	console.log(obj[i]);
				// }

                cb(obj)
			}
        }
    });
};

// post(j)